package com.jiangjiang.eshop.inventory.updater;

import com.jiangjiang.eshop.common.help.DateHelper;
import com.jiangjiang.eshop.inventory.domain.GoodsStockEntity;
import com.jiangjiang.eshop.inventory.service.InventoryGoodsStockService;
import com.jiangjiang.eshop.order.dto.OrderInfoDTO;
import com.jiangjiang.eshop.order.dto.OrderItemDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @ClassName PayOrderStockUpdaterFactory
 * @Description TODO
 * @Author jhw
 * @Date 2021/6/14 13:22
 * @Version 1.0
 **/
@Service
public class PayOrderStockUpdaterFactory<T> extends AbstractStockUpdaterFactory<T>{
    /**
     * 构造方法
     *
     * @param goodsStockService
     * @param dateHelper
     */
    @Autowired
    public PayOrderStockUpdaterFactory(
            InventoryGoodsStockService goodsStockService,
            DateHelper dateHelper) {
        super(goodsStockService, dateHelper);
    }

    @Override
    protected List<Long> getGoodsSkuIds(T parameter) throws Exception {
        OrderInfoDTO infoDTO = (OrderInfoDTO) parameter;
        return infoDTO.getOrderItems().stream().map(OrderItemDTO::getGoodsId).collect(Collectors.toList());
    }

    @Override
    protected StockUpdater createUpdater(List<GoodsStockEntity> goodsStock, T parameter) {
        OrderInfoDTO infoDTO = (OrderInfoDTO) parameter;
        Map<Long, OrderItemDTO> itemDTOMap = infoDTO.getOrderItems()
                .stream()
                .collect(Collectors.toMap(OrderItemDTO::getGoodsId, Function.identity()));

        return new PayOrderStockUpdater(goodsStockService,goodsStock,dateHelper,itemDTOMap);
    }
}
